<#include "macro_.ftl">
package ${packageCode}.${moduleMapperKey}.controller;

import java.util.List;
import java.util.Map;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cardone.context.Constants;
import com.cardone.common.dto.PaginationDto;
<#list entityMapperList as entityMapper>
<#if entityMapper.markService && entityMapper.markController>
import ${packageCode}.${moduleMapperKey}.dto.${entityMapper.code}Dto;
</#if>
</#list>
import com.cardone.platform.configuration.service.ConfigurationService;
<#list entityMapperList as entityMapper>
<#if entityMapper.markService && entityMapper.markController>
import ${packageCode}.${moduleMapperKey}.validator.${entityMapper.code}Validator;
</#if>
</#list>
import ${packageCode}.${moduleMapperKey}.vo.${businessCode}Vo;
import ${packageCode}.${moduleMapperKey}.service.${moduleMapperKey?cap_first}Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * ${businessMapper.name!businessCode}
 *
 * @author ${author!'yaoht'}
 */
@Setter
@Slf4j
@RequestMapping(${businessCode}Controller.Paths.ROOT)
@Controller
public class ${businessCode}Controller {
	/**
	 * 路径集合
	 * 
	 * @author yaoht
	 * 
	 */
	public enum Paths {
<#list entityMapperList as entityMapper>
<#if entityMapper.markService && entityMapper.markController>
		/**
		 * ${entityMapper.remarks!entityMapper.code}编辑视图路径
		 */
		${entityMapper.tableCode?upper_case}Edit("${entityMapper.code?uncap_first}/edit"),
	
		/**
		 * ${entityMapper.remarks!entityMapper.code}主界面视图路径
		 */
		${entityMapper.tableCode?upper_case}Index("${entityMapper.code?uncap_first}/index"),
		
		/**
		 * ${entityMapper.remarks!entityMapper.code}编辑视图路径
		 */
		${entityMapper.tableCode?upper_case}New("${entityMapper.code?uncap_first}/new"),
		
		/**
		 * ${entityMapper.remarks!entityMapper.code}查看编辑视图路径
		 */
		${entityMapper.tableCode?upper_case}View("${entityMapper.code?uncap_first}/view"),
		
</#if>
</#list>
		/**
		 * ${businessMapper.name!businessCode}主界面
		 */
		 index;

		/**
		 * ${businessMapper.name!businessCode}视图根目录
		 */
		public static final String ROOT = "/${projectCode!}/${moduleMapperKey}/${businessCode?uncap_first}/";

		/**
		 * 路径
		 */
		private final String path;

		/**
		 * 构建
		 * 
		 */
		private Paths() {
			this.path = Paths.ROOT + this.name();
		}

		/**
		 * 构建
		 * 
		 * @param path
		 *            路径
		 */
		private Paths(final String path) {
			this.path = Paths.ROOT + path;
		}

		/**
		 * 获取
		 * 
		 * @return 路径
		 */
		public String path() {
			return this.path;
		}
	}

	/**
	 * models集合
	 * 
	 * @author yaoht
	 * 
	 */
	public enum models {
<#list entityMapperList as entityMapper>
<#if entityMapper.markService && entityMapper.markController>
		/**
		 * ${entityMapper.remarks!entityMapper.code}
		 */
		${entityMapper.code?uncap_first},
		
</#if>
</#list>
		/**
		 * 消息
		 */
		 message;
	}

	/**
	 * ${businessMapper.name!businessCode}服务
	 */
	@Qualifier(${moduleMapperKey?cap_first}Service.BEAN_ID)
	@Autowired(required = false)
	private ${moduleMapperKey?cap_first}Service ${moduleMapperKey}Service;
<#list entityMapperList as entityMapper>
<#if entityMapper.markService && entityMapper.markController>

	/**
	 * ${entityMapper.remarks!entityMapper.code}验证
	 */
	@Qualifier(${entityMapper.code}Validator.BEAN_ID)
	@Autowired(required = false)
	public Validator ${entityMapper.code?uncap_first}Validator;
</#if>
</#list>
<#list entityMapperList as entityMapper>
<#if entityMapper.markService && entityMapper.markController>

	/**
	 * ${entityMapper.remarks!entityMapper.code}：删除
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> ${entityMapper.code?uncap_first}Delete(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.${moduleMapperKey}Service.get${businessCode}Service().delete${entityMapper.code}ByIds(${businessCode?uncap_first}Vo.get${entityMapper.code}());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (${businessCode}Controller.log.isDebugEnabled()) {
				${businessCode}Controller.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：更新
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/update.json" }, method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> ${entityMapper.code?uncap_first}Update(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.${moduleMapperKey}Service.get${businessCode}Service().update${entityMapper.code}ByIds(${businessCode?uncap_first}Vo.get${entityMapper.code}());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (${businessCode}Controller.log.isDebugEnabled()) {
				${businessCode}Controller.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：编辑
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @param model
	 *            上下文映射
	 * @return ${entityMapper.remarks!entityMapper.code}编辑视图路径
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/edit.html" }, method = RequestMethod.GET)
	public String ${entityMapper.code?uncap_first}Edit(final ${businessCode}Vo ${businessCode?uncap_first}Vo, final Model model) {
		final ${entityMapper.code}Dto ${entityMapper.code?uncap_first} = this.${moduleMapperKey}Service.get${businessCode}Service().
				find${entityMapper.code}ById(${businessCode?uncap_first}Vo.get${entityMapper.code}());

		model.addAttribute(${businessCode}Controller.models.${entityMapper.code?uncap_first}.name(), ${entityMapper.code?uncap_first});

		return ${businessCode}Controller.Paths.${entityMapper.tableCode?upper_case}Edit.path();
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：主界面
	 * 
	 * @return ${entityMapper.remarks!entityMapper.code}主界面视图路径
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/index.html" })
	public String ${entityMapper.code?uncap_first}Index() {
		return ${businessCode}Controller.Paths.${entityMapper.tableCode?upper_case}Index.path();
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：主界面分页查询
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/index/pagination.json" })
	@ResponseBody
    public Map<String, Object> ${entityMapper.code?uncap_first}IndexPagination(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		final PaginationDto<${entityMapper.code}Dto> ${entityMapper.code?uncap_first}Pagination = this.${moduleMapperKey}Service.get${businessCode}Service()
				.pagination${entityMapper.code}ByCode(${businessCode?uncap_first}Vo.get${entityMapper.code}());

		final Map<String, Object> ${entityMapper.code?uncap_first}PaginationMap = Maps.newHashMap();

		${entityMapper.code?uncap_first}PaginationMap.put(Constants.total.stringValue(), ${entityMapper.code?uncap_first}Pagination.getTotalSize());
		${entityMapper.code?uncap_first}PaginationMap.put(Constants.rows.stringValue(), ${entityMapper.code?uncap_first}Pagination.getDataList());

		return ${entityMapper.code?uncap_first}PaginationMap;
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：新增
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @param model
	 *            上下文映射
	 * @return ${entityMapper.remarks!entityMapper.code}编辑视图路径
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/new.html" }, method = RequestMethod.GET)
	public String ${entityMapper.code?uncap_first}New(final ${businessCode}Vo ${businessCode?uncap_first}Vo, final Model model) {
		model.addAttribute(${businessCode}Controller.models.${entityMapper.code?uncap_first}.name(), ${businessCode?uncap_first}Vo.get${entityMapper.code}());

		return ${businessCode}Controller.Paths.${entityMapper.tableCode?upper_case}New.path();
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：保存
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
    public String ${entityMapper.code?uncap_first}Save(final ${businessCode}Vo ${businessCode?uncap_first}Vo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.${entityMapper.code?uncap_first}Validator.validate(${businessCode?uncap_first}Vo.get${entityMapper.code}(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.${moduleMapperKey}Service.get${businessCode}Service().save${entityMapper.code}ByIdOrCode(${businessCode?uncap_first}Vo.get${entityMapper.code}());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (${businessCode}Controller.log.isDebugEnabled()) {
				${businessCode}Controller.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：验证
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/validationByCode.json" })
	@ResponseBody
    public Boolean ${entityMapper.code?uncap_first}ValidationByCode(final ${businessCode}Vo ${businessCode?uncap_first}Vo) {
		try {
			final int count = this.${moduleMapperKey}Service.get${businessCode}Service().read${entityMapper.code}ByCodeNqId(
					${businessCode?uncap_first}Vo.get${entityMapper.code}());

			return (count < 1);
		} catch (final Exception e) {
			if (${businessCode}Controller.log.isDebugEnabled()) {
				${businessCode}Controller.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * ${entityMapper.remarks!entityMapper.code}：查看
	 * 
	 * @param ${businessCode?uncap_first}Vo
	 *            ${entityMapper.remarks!entityMapper.code}视图对象
	 * @param model
	 *            上下文映射
	 * @return ${entityMapper.remarks!entityMapper.code}查看编辑视图路径
	 */
	@RequestMapping(value = { "${entityMapper.code?uncap_first}/view.html" }, method = RequestMethod.GET)
	public String ${entityMapper.code?uncap_first}View(final ${businessCode}Vo ${businessCode?uncap_first}Vo, final Model model) {
		final ${entityMapper.code}Dto ${entityMapper.code?uncap_first} = this.${moduleMapperKey}Service.get${businessCode}Service().
					find${entityMapper.code}ById(${businessCode?uncap_first}Vo.get${entityMapper.code}());

		model.addAttribute(${businessCode}Controller.models.${entityMapper.code?uncap_first}.name(), ${entityMapper.code?uncap_first});

		return ${businessCode}Controller.Paths.${entityMapper.tableCode?upper_case}View.path();
	}
</#if>
</#list>
}
